//
//  SGWiFiView.m
//  SGWiFiUpload
//
//  Created by soulghost on 30/6/2016.
//  Copyright © 2016 soulghost. All rights reserved.
//

#import "SGWiFiView.h"
#import <YYKit/YYKit.h>
@interface SGWiFiView ()
@property (nonatomic, weak) UIImageView *imageView;
@property (nonatomic, assign) CGSize imageSize;
@property (nonatomic, weak) UIScrollView *scrollView;
@property(copy, nonatomic) NSString *tempAddress;
@property (nonatomic, weak) YYLabel *descLabel;
@end

@implementation SGWiFiView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self commonInit];
    }
    return self;
}

- (NSMutableAttributedString *)descTextWithAddress:(NSString *)address {
    NSString *wholeText = [NSString stringWithFormat:@"1⃣️、请确保手机和电脑连接相同WiFi。\nPlease make sure your iPhone and PC is connected to the same WiFi.\n\n2⃣️、打开浏览器输入下面👇地址：\nOpen the URL below in a browser:\n\n点击复制:%@\n\n3⃣️、点击提交按钮，等待上传完成。\n When click the submit button on the website, please wait until the page finish loading.",address];
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithString:wholeText];
    text.font = [UIFont systemFontOfSize:17];
    NSRange range = [wholeText rangeOfString:address];
    [text addAttribute:NSUnderlineStyleAttributeName value:
     [NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
    [text addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#3B3B3B"]} range:NSMakeRange(0, wholeText.length)];
    [text setTextHighlightRange:range
                          color:[UIColor orangeColor]
                backgroundColor:[UIColor whiteColor]
                      tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
                          self.addressAction(address);
                      }];
    return text;
}

- (void)commonInit {
    self.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1.0];
    UIScrollView *scrollView = [UIScrollView new];
    self.scrollView = scrollView;
    [self addSubview:scrollView];
    UIImage *image = [UIImage imageNamed:@"wifi.png"];
    self.imageSize = image.size;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    self.imageView = imageView;
    [self.scrollView addSubview:imageView];
    
    YYLabel *descLabel = [YYLabel new];
    descLabel.font = [UIFont systemFontOfSize:15];
    descLabel.numberOfLines = 0;
    descLabel.attributedText = [self descTextWithAddress:@"Fetching..."];
    self.descLabel = descLabel;
    [self.scrollView addSubview:descLabel];
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [self.scrollView addSubview:nameLabel];
    nameLabel.textColor = [UIColor orangeColor];//[UIColor colorWithHexString:@"#EE5C42"];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:17];
    nameLabel.numberOfLines = 0;
    self.nameLabel = nameLabel;
}

- (void)setAddress:(NSString *)address {
    if (!address) { return;}
    self.tempAddress = address;
    self.descLabel.attributedText = [self descTextWithAddress:address];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if (!self.imageSize.width || !self.imageSize.height || !self.descLabel.font) {
        return;
    }
    CGSize contentSize = self.frame.size;
    self.scrollView.frame = self.bounds;
    CGFloat imageW = contentSize.width * 0.8f;
    CGFloat display2RealFactor = imageW / self.imageSize.width;
    CGFloat imageH = self.imageSize.height * display2RealFactor;
    CGRect imageFrame = self.imageView.frame;
    imageFrame.size = CGSizeMake(imageW, imageH);
    self.imageView.frame = imageFrame;
    self.imageView.center = CGPointMake(contentSize.width * 0.5f, imageH * 0.5f + 60);
    CGSize labelSize = [self.descLabel.text boundingRectWithSize:CGSizeMake(contentSize.width - 30, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.descLabel.font} context:nil].size;
    CGFloat labelW = labelSize.width;
    CGFloat labelH = labelSize.height+50;
    CGFloat labelX = (contentSize.width - labelW) * 0.5f;
    CGFloat labelY = CGRectGetMaxY(self.imageView.frame) + 20;
    self.descLabel.frame = CGRectMake(labelX, labelY, labelW, labelH);
    self.nameLabel.frame = CGRectMake(labelX, CGRectGetMaxY(self.descLabel.frame), labelW, 50);
    self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.descLabel.frame) + 20);
}


@end
